{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.naive_bayes import MultinomialNB, BernoulliNB\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "posting_list = [\n",
    "        ['my', 'dog', 'has', 'flea', 'problems', 'help', 'please'],\n",
    "        ['maybe', 'not', 'take', 'him', 'to', 'dog', 'park', 'stupid'],\n",
    "        ['my', 'dalmation', 'is', 'so', 'cute', 'I', 'love', 'him'],\n",
    "        ['stop', 'posting', 'stupid', 'worthless', 'garbage'],\n",
    "        ['mr', 'licks', 'ate', 'my', 'steak', 'how', 'to', 'stop', 'him'],\n",
    "        ['quit', 'buying', 'worthless', 'dog', 'food', 'stupid'],\n",
    "        ['i', 'want', 'to', 'hear', 'stories', 'about', 'your', 'dogs'],\n",
    "        ['you', 'shut', 'up', 'you', 'idiot']\n",
    "    ]\n",
    "class_vec = [0, 1, 0, 1, 0, 1, 0, 1]\n",
    "\n",
    "test_set = [\n",
    "    ['love', 'my', 'dalmation'],\n",
    "    ['stupid', 'garbage'],\n",
    "    ['stop', 'posting', 'about', 'your', 'idiot', 'dog'],\n",
    "    # ['some', 'text', 'not', 'important'],\n",
    "]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 计数 + 多项式朴素贝叶斯"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def createVocabList(data_set):\n",
    "    vocab_set = set([])\n",
    "    for document in data_set:\n",
    "        vocab_set = vocab_set | set(document)\n",
    "    return list(vocab_set)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from collections import Counter\n",
    "def setOfWords2Count(vocab_list, input_set):\n",
    "    cnt = Counter(input_set)\n",
    "    return [cnt[x] for x in vocab_list]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>ate</th>\n",
       "      <th>my</th>\n",
       "      <th>dalmation</th>\n",
       "      <th>stupid</th>\n",
       "      <th>so</th>\n",
       "      <th>him</th>\n",
       "      <th>your</th>\n",
       "      <th>shut</th>\n",
       "      <th>flea</th>\n",
       "      <th>licks</th>\n",
       "      <th>...</th>\n",
       "      <th>is</th>\n",
       "      <th>buying</th>\n",
       "      <th>up</th>\n",
       "      <th>I</th>\n",
       "      <th>love</th>\n",
       "      <th>cute</th>\n",
       "      <th>you</th>\n",
       "      <th>garbage</th>\n",
       "      <th>steak</th>\n",
       "      <th>i</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">0</th>\n",
       "      <th>i want to hear stories about your dogs</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mr licks ate my steak how to stop him</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>my dalmation is so cute I love him</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>my dog has flea problems help please</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">1</th>\n",
       "      <th>maybe not take him to dog park stupid</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>quit buying worthless dog food stupid</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>stop posting stupid worthless garbage</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>you shut up you idiot</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>8 rows × 43 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                          ate  my  dalmation  stupid  so  him  \\\n",
       "0 i want to hear stories about your dogs    0   0          0       0   0    0   \n",
       "  mr licks ate my steak how to stop him     1   1          0       0   0    1   \n",
       "  my dalmation is so cute I love him        0   1          1       0   1    1   \n",
       "  my dog has flea problems help please      0   1          0       0   0    0   \n",
       "1 maybe not take him to dog park stupid     0   0          0       1   0    1   \n",
       "  quit buying worthless dog food stupid     0   0          0       1   0    0   \n",
       "  stop posting stupid worthless garbage     0   0          0       1   0    0   \n",
       "  you shut up you idiot                     0   0          0       0   0    0   \n",
       "\n",
       "                                          your  shut  flea  licks ...  is  \\\n",
       "0 i want to hear stories about your dogs     1     0     0      0 ...   0   \n",
       "  mr licks ate my steak how to stop him      0     0     0      1 ...   0   \n",
       "  my dalmation is so cute I love him         0     0     0      0 ...   1   \n",
       "  my dog has flea problems help please       0     0     1      0 ...   0   \n",
       "1 maybe not take him to dog park stupid      0     0     0      0 ...   0   \n",
       "  quit buying worthless dog food stupid      0     0     0      0 ...   0   \n",
       "  stop posting stupid worthless garbage      0     0     0      0 ...   0   \n",
       "  you shut up you idiot                      0     1     0      0 ...   0   \n",
       "\n",
       "                                          buying  up  I  love  cute  you  \\\n",
       "0 i want to hear stories about your dogs       0   0  0     0     0    0   \n",
       "  mr licks ate my steak how to stop him        0   0  0     0     0    0   \n",
       "  my dalmation is so cute I love him           0   0  1     1     1    0   \n",
       "  my dog has flea problems help please         0   0  0     0     0    0   \n",
       "1 maybe not take him to dog park stupid        0   0  0     0     0    0   \n",
       "  quit buying worthless dog food stupid        1   0  0     0     0    0   \n",
       "  stop posting stupid worthless garbage        0   0  0     0     0    0   \n",
       "  you shut up you idiot                        0   1  0     0     0    2   \n",
       "\n",
       "                                          garbage  steak  i  \n",
       "0 i want to hear stories about your dogs        0      0  1  \n",
       "  mr licks ate my steak how to stop him         0      1  0  \n",
       "  my dalmation is so cute I love him            0      0  0  \n",
       "  my dog has flea problems help please          0      0  0  \n",
       "1 maybe not take him to dog park stupid         0      0  0  \n",
       "  quit buying worthless dog food stupid         0      0  0  \n",
       "  stop posting stupid worthless garbage         1      0  0  \n",
       "  you shut up you idiot                         0      0  0  \n",
       "\n",
       "[8 rows x 43 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vocab_list = createVocabList(posting_list)\n",
    "vocab_cnt = list(map(lambda post: setOfWords2Count(vocab_list, post), posting_list))\n",
    "X = np.array(vocab_cnt)\n",
    "Y = np.array(class_vec)\n",
    "df = pd.DataFrame(X, columns=vocab_list, index=[Y, [' '.join(post) for post in posting_list]]).sort_index()\n",
    "# df['is_offensive'] = Y\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "class 0 word count: 32\n",
      "class 1 word count: 24\n",
      "Pk0    1.0\n",
      "Pk1    1.0\n",
      "dtype: float64\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>please</th>\n",
       "      <th>you</th>\n",
       "      <th>I</th>\n",
       "      <th>has</th>\n",
       "      <th>to</th>\n",
       "      <th>hear</th>\n",
       "      <th>ate</th>\n",
       "      <th>your</th>\n",
       "      <th>dalmation</th>\n",
       "      <th>not</th>\n",
       "      <th>...</th>\n",
       "      <th>garbage</th>\n",
       "      <th>dog</th>\n",
       "      <th>love</th>\n",
       "      <th>cute</th>\n",
       "      <th>food</th>\n",
       "      <th>i</th>\n",
       "      <th>posting</th>\n",
       "      <th>licks</th>\n",
       "      <th>park</th>\n",
       "      <th>problems</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Pk0</th>\n",
       "      <td>0.026667</td>\n",
       "      <td>0.013333</td>\n",
       "      <td>0.026667</td>\n",
       "      <td>0.026667</td>\n",
       "      <td>0.040000</td>\n",
       "      <td>0.026667</td>\n",
       "      <td>0.026667</td>\n",
       "      <td>0.026667</td>\n",
       "      <td>0.026667</td>\n",
       "      <td>0.013333</td>\n",
       "      <td>...</td>\n",
       "      <td>0.013333</td>\n",
       "      <td>0.026667</td>\n",
       "      <td>0.026667</td>\n",
       "      <td>0.026667</td>\n",
       "      <td>0.013333</td>\n",
       "      <td>0.026667</td>\n",
       "      <td>0.013333</td>\n",
       "      <td>0.026667</td>\n",
       "      <td>0.013333</td>\n",
       "      <td>0.026667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Pk1</th>\n",
       "      <td>0.014925</td>\n",
       "      <td>0.044776</td>\n",
       "      <td>0.014925</td>\n",
       "      <td>0.014925</td>\n",
       "      <td>0.029851</td>\n",
       "      <td>0.014925</td>\n",
       "      <td>0.014925</td>\n",
       "      <td>0.014925</td>\n",
       "      <td>0.014925</td>\n",
       "      <td>0.029851</td>\n",
       "      <td>...</td>\n",
       "      <td>0.029851</td>\n",
       "      <td>0.044776</td>\n",
       "      <td>0.014925</td>\n",
       "      <td>0.014925</td>\n",
       "      <td>0.029851</td>\n",
       "      <td>0.014925</td>\n",
       "      <td>0.029851</td>\n",
       "      <td>0.014925</td>\n",
       "      <td>0.029851</td>\n",
       "      <td>0.014925</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2 rows × 43 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       please       you         I       has        to      hear       ate  \\\n",
       "Pk0  0.026667  0.013333  0.026667  0.026667  0.040000  0.026667  0.026667   \n",
       "Pk1  0.014925  0.044776  0.014925  0.014925  0.029851  0.014925  0.014925   \n",
       "\n",
       "         your  dalmation       not  ...   garbage       dog      love  \\\n",
       "Pk0  0.026667   0.026667  0.013333  ...  0.013333  0.026667  0.026667   \n",
       "Pk1  0.014925   0.014925  0.029851  ...  0.029851  0.044776  0.014925   \n",
       "\n",
       "         cute      food         i   posting     licks      park  problems  \n",
       "Pk0  0.026667  0.013333  0.026667  0.013333  0.026667  0.013333  0.026667  \n",
       "Pk1  0.014925  0.029851  0.014925  0.029851  0.014925  0.029851  0.014925  \n",
       "\n",
       "[2 rows x 43 columns]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print('class 0 word count:', df.loc[0,].sum().sum())\n",
    "print('class 1 word count:', df.loc[1,].sum().sum())\n",
    "alpha = 1.0\n",
    "Pk0 = (df.loc[0,].sum() + alpha)/(df.loc[0,].sum().sum() + alpha * len(df.columns))\n",
    "Pk1 = (df.loc[1,].sum() + alpha)/(df.loc[1,].sum().sum() + alpha * len(df.columns))\n",
    "\n",
    "P = pd.DataFrame([Pk0, Pk1], index=['Pk0', 'Pk1'])\n",
    "print(P.sum(axis=1))\n",
    "P"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "MultinomialNB(alpha=1.0, class_prior=None, fit_prior=True)"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf = MultinomialNB()\n",
    "clf.fit(X, Y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 1])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test = np.array(list(map(lambda post: setOfWords2Count(vocab_list, post), test_set)))\n",
    "clf.predict(test)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 词向量 + 伯努利朴素贝叶斯"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "def setOfWords2Vec(vocab_list, input_set):\n",
    "    return_vec = [0] * len(vocab_list)\n",
    "    for word in input_set:\n",
    "        assert word in vocab_list, \"the word: %s is not in my Vocabulary!\" % (word,)\n",
    "        return_vec[vocab_list.index(word)] = 1\n",
    "    return return_vec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>please</th>\n",
       "      <th>you</th>\n",
       "      <th>I</th>\n",
       "      <th>has</th>\n",
       "      <th>to</th>\n",
       "      <th>hear</th>\n",
       "      <th>ate</th>\n",
       "      <th>your</th>\n",
       "      <th>dalmation</th>\n",
       "      <th>not</th>\n",
       "      <th>...</th>\n",
       "      <th>garbage</th>\n",
       "      <th>dog</th>\n",
       "      <th>love</th>\n",
       "      <th>cute</th>\n",
       "      <th>food</th>\n",
       "      <th>i</th>\n",
       "      <th>posting</th>\n",
       "      <th>licks</th>\n",
       "      <th>park</th>\n",
       "      <th>problems</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">0</th>\n",
       "      <th>i want to hear stories about your dogs</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mr licks ate my steak how to stop him</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>my dalmation is so cute I love him</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>my dog has flea problems help please</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"4\" valign=\"top\">1</th>\n",
       "      <th>maybe not take him to dog park stupid</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>quit buying worthless dog food stupid</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>stop posting stupid worthless garbage</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>you shut up you idiot</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>8 rows × 43 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                          please  you  I  has  to  hear  ate  \\\n",
       "0 i want to hear stories about your dogs       0    0  0    0   1     1    0   \n",
       "  mr licks ate my steak how to stop him        0    0  0    0   1     0    1   \n",
       "  my dalmation is so cute I love him           0    0  1    0   0     0    0   \n",
       "  my dog has flea problems help please         1    0  0    1   0     0    0   \n",
       "1 maybe not take him to dog park stupid        0    0  0    0   1     0    0   \n",
       "  quit buying worthless dog food stupid        0    0  0    0   0     0    0   \n",
       "  stop posting stupid worthless garbage        0    0  0    0   0     0    0   \n",
       "  you shut up you idiot                        0    1  0    0   0     0    0   \n",
       "\n",
       "                                          your  dalmation  not  ...  garbage  \\\n",
       "0 i want to hear stories about your dogs     1          0    0  ...        0   \n",
       "  mr licks ate my steak how to stop him      0          0    0  ...        0   \n",
       "  my dalmation is so cute I love him         0          1    0  ...        0   \n",
       "  my dog has flea problems help please       0          0    0  ...        0   \n",
       "1 maybe not take him to dog park stupid      0          0    1  ...        0   \n",
       "  quit buying worthless dog food stupid      0          0    0  ...        0   \n",
       "  stop posting stupid worthless garbage      0          0    0  ...        1   \n",
       "  you shut up you idiot                      0          0    0  ...        0   \n",
       "\n",
       "                                          dog  love  cute  food  i  posting  \\\n",
       "0 i want to hear stories about your dogs    0     0     0     0  1        0   \n",
       "  mr licks ate my steak how to stop him     0     0     0     0  0        0   \n",
       "  my dalmation is so cute I love him        0     1     1     0  0        0   \n",
       "  my dog has flea problems help please      1     0     0     0  0        0   \n",
       "1 maybe not take him to dog park stupid     1     0     0     0  0        0   \n",
       "  quit buying worthless dog food stupid     1     0     0     1  0        0   \n",
       "  stop posting stupid worthless garbage     0     0     0     0  0        1   \n",
       "  you shut up you idiot                     0     0     0     0  0        0   \n",
       "\n",
       "                                          licks  park  problems  \n",
       "0 i want to hear stories about your dogs      0     0         0  \n",
       "  mr licks ate my steak how to stop him       1     0         0  \n",
       "  my dalmation is so cute I love him          0     0         0  \n",
       "  my dog has flea problems help please        0     0         1  \n",
       "1 maybe not take him to dog park stupid       0     1         0  \n",
       "  quit buying worthless dog food stupid       0     0         0  \n",
       "  stop posting stupid worthless garbage       0     0         0  \n",
       "  you shut up you idiot                       0     0         0  \n",
       "\n",
       "[8 rows x 43 columns]"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vocab_list = createVocabList(posting_list)\n",
    "vocab_vec = list(map(lambda post: setOfWords2Vec(vocab_list, post), posting_list))\n",
    "X = np.array(vocab_vec)\n",
    "Y = np.array(class_vec)\n",
    "df = pd.DataFrame(X, columns=vocab_list, index=[Y, [' '.join(post) for post in posting_list]]).sort_index()\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>please</th>\n",
       "      <th>you</th>\n",
       "      <th>I</th>\n",
       "      <th>has</th>\n",
       "      <th>to</th>\n",
       "      <th>hear</th>\n",
       "      <th>ate</th>\n",
       "      <th>your</th>\n",
       "      <th>dalmation</th>\n",
       "      <th>not</th>\n",
       "      <th>...</th>\n",
       "      <th>garbage</th>\n",
       "      <th>dog</th>\n",
       "      <th>love</th>\n",
       "      <th>cute</th>\n",
       "      <th>food</th>\n",
       "      <th>i</th>\n",
       "      <th>posting</th>\n",
       "      <th>licks</th>\n",
       "      <th>park</th>\n",
       "      <th>problems</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.166667</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.500000</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.166667</td>\n",
       "      <td>...</td>\n",
       "      <td>0.166667</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.166667</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.166667</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.166667</td>\n",
       "      <td>0.333333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.166667</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.166667</td>\n",
       "      <td>0.166667</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.166667</td>\n",
       "      <td>0.166667</td>\n",
       "      <td>0.166667</td>\n",
       "      <td>0.166667</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>...</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.500000</td>\n",
       "      <td>0.166667</td>\n",
       "      <td>0.166667</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.166667</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.166667</td>\n",
       "      <td>0.333333</td>\n",
       "      <td>0.166667</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2 rows × 43 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     please       you         I       has        to      hear       ate  \\\n",
       "0  0.333333  0.166667  0.333333  0.333333  0.500000  0.333333  0.333333   \n",
       "1  0.166667  0.333333  0.166667  0.166667  0.333333  0.166667  0.166667   \n",
       "\n",
       "       your  dalmation       not  ...   garbage       dog      love      cute  \\\n",
       "0  0.333333   0.333333  0.166667  ...  0.166667  0.333333  0.333333  0.333333   \n",
       "1  0.166667   0.166667  0.333333  ...  0.333333  0.500000  0.166667  0.166667   \n",
       "\n",
       "       food         i   posting     licks      park  problems  \n",
       "0  0.166667  0.333333  0.166667  0.333333  0.166667  0.333333  \n",
       "1  0.333333  0.166667  0.333333  0.166667  0.333333  0.166667  \n",
       "\n",
       "[2 rows x 43 columns]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "alpha = 1\n",
    "Pk0 = (df.loc[0,].sum() + alpha)/(len(df.loc[0,]) + alpha * 2)\n",
    "Pk1 = (df.loc[1,].sum() + alpha)/(len(df.loc[1,]) + alpha * 2)\n",
    "pd.DataFrame([Pk0, Pk1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "BernoulliNB(alpha=1.0, binarize=0.0, class_prior=None, fit_prior=True)"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "clf = BernoulliNB()\n",
    "clf.fit(X, Y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 1, 1])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test = np.array(list(map(lambda post: setOfWords2Vec(vocab_list, post), test_set)))\n",
    "clf.predict(test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
